package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.OrderDetailBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.OrderService;
import com.squareup.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.administrator.yuejian_2.R.layout.activity_orderdetail;

public class OrderDetailActivity extends AppCompatActivity {

    private int oid;
    private ImageView imageView;
    private TextView aname;
    private TextView amount;
    private TextView summoney;
    private TextView opayway;
    private TextView onumber;
    private TextView otime;
    private Context context;
    private ImageButton back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderdetail);

        oid = getIntent().getIntExtra("oid",-1);
        imageView = (ImageView) findViewById(R.id.orderdetail_img);
        aname = (TextView) findViewById(R.id.orderdetail_acname);
        amount = (TextView) findViewById(R.id.orderdetail_amount);
        summoney = (TextView) findViewById(R.id.orderdetail_sum);
        opayway = (TextView) findViewById(R.id.orderdetail_payway);
        onumber = (TextView) findViewById(R.id.orderdetail_number);
        otime = (TextView) findViewById(R.id.orderdetail_time);

        orderdetail();

        back = (ImageButton) findViewById(R.id.orderdetail_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


    }

    public void orderdetail(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        OrderService service = retrofit.create(OrderService.class);
        Call<OrderDetailBean> call = service.orderdetail(oid);
        call.enqueue(new Callback<OrderDetailBean>() {
            @Override
            public void onResponse(Call<OrderDetailBean> call, Response<OrderDetailBean> response) {
                if (response.body() != null){
                    String pic = response.body().getA_pic();
                    String name= response.body().getA_name();
                    int m = response.body().getAmount();
                    String payway = response.body().getPayway();
                    String nu = response.body().getOnumber();
                    String time = response.body().getTime();
                    int mo = response.body().getA_money();

                    int sumoney = m * mo ;
                    String su = String.valueOf(sumoney);
                    String a = String.valueOf(m);

                    if (!pic.equals(""))
                        Picasso.with(context)
                                .load("http://10.0.2.2/YueJian16/Public/activityphoto/"+pic)
                                .into(imageView);
                    aname.setText(name);
                    amount.setText(a);
                    summoney.setText("￥"+ su);
                    opayway.setText(payway);
                    onumber.setText(nu);
                    otime.setText(time);


                }
            }

            @Override
            public void onFailure(Call<OrderDetailBean> call, Throwable t) {

            }
        });

    }
}
